﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace e3
{
    public partial class Form1 : Form
    {
        private string file_doc1;
        private string file_doc2;
        private string newfile;
        public Form1()
        {
            InitializeComponent();
            /*
            doc1.Items.Add("test1.txt");
            doc1.Items.Add("test2.txt");
            doc1.Items.Add("test3.txt");
            doc1.Items.Add("test4.txt");
            doc2.Items.Add("test1.txt");
            doc2.Items.Add("test2.txt");
            doc2.Items.Add("test3.txt");
            doc2.Items.Add("test4.txt");*/


        }
        
        

        private void button1_Click(object sender, EventArgs e)
        {//点击合并按钮
            try
            {
                FileStream fin1 = new FileStream(this.file_doc1,FileMode.Open,FileAccess.Read);
                FileStream fin2 = new FileStream(this.file_doc2, FileMode.Open, FileAccess.Read);
                FileStream fout = new FileStream(this.newfile, FileMode.Create, FileAccess.Write);
                //int lineNum = 0;
                using (StreamReader reader1 = new StreamReader(fin1, Encoding.UTF8)) {
                    using(StreamReader reader2=new StreamReader(fin2, Encoding.UTF8))
                    { 
                        using (StreamWriter writer = new StreamWriter(fout, Encoding.UTF8))
                        {
                            string s = null;
                            while ((s = reader1.ReadLine()) != null)
                            {//读取第一个文件
                                //lineNum++;
                                writer.WriteLine(s);
                                //写入新文件
                            }
                            while ((s = reader2.ReadLine())!=null)
                            {//读取第二个文件
                                //lineNum++;
                                writer.WriteLine(s);
                                //写入新文件
                            }
                            MessageBox.Show("合并成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        };
                    } ;
                };
            }
            catch(FileNotFoundException ex)
            {
                Console.WriteLine($"file{ex.FileName}does not exist");
                MessageBox.Show("合并失败，找不到文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex1)
            {
                MessageBox.Show("合并失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine(ex1);
            }
        }

        

        private void filename_TextChanged(object sender, EventArgs e)
        {
            this.newfile = @"data\" + filename.Text;


        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog saveFile = new OpenFileDialog();
            saveFile.Filter = "所有txt文件|*.txt";//设置文件类型
            saveFile.Title = "导出数据";//设置标题

            saveFile.AddExtension = true;//是否自动增加所辍名
            saveFile.AutoUpgradeEnabled = true;//是否随系统升级而升级外观
            if (saveFile.ShowDialog() == DialogResult.OK)//如果点的是确定就得到文件路径
            {
                this.doc1.Text = saveFile.FileName;//得到文件路径
                //MessageBox.Show(FileName);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog saveFile = new OpenFileDialog();
            saveFile.Filter = "所有txt文件|*.txt";//设置文件类型
            saveFile.Title = "导出数据";//设置标题

            saveFile.AddExtension = true;//是否自动增加所辍名
            saveFile.AutoUpgradeEnabled = true;//是否随系统升级而升级外观
            if (saveFile.ShowDialog() == DialogResult.OK)//如果点的是确定就得到文件路径
            {
                this.doc2.Text = saveFile.FileName;//得到文件路径
                //MessageBox.Show(FileName);
            }
        }

        private void doc1_TextChanged(object sender, EventArgs e)
        {
            this.file_doc1 = doc1.Text;
        }

        private void doc2_TextChanged(object sender, EventArgs e)
        {
            this.file_doc2 = doc2.Text;
        }
    }
}
